// pages/businessCard/index.js
import {
  upload
} from '../../../services/_utils/request';

import businessCardApi from '../../../services/businessCard/businessCard';


Page({

  /**
   * 页面的初始数据
   */
  data: {
    val: false,
    teacher: {
      isOpen: true,
      userId: null,
      id: null,
      teacherName: '',
      title: '',
      company: '',
      position: '',
      icon: null,
      industry: '',
      phone: '',
      wx: '',
      skills: '',
      address: '',
      price: 0,
      contents: [{
          type: 'txt',
          content: ''
        },
        {
          type: 'img',
          content: ''
        },
      ]
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this
    let userId = getApp().globalData.userInfo.userId
    businessCardApi.findByUserId(userId).then(res => {
      console.log("ok")
      console.log(res)
      if (res) {
        console.log("res")
        that.setData({
          teacher: res
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  uploadIcon() {
    var that = this
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      sourceType: ['album', 'camera'],
      maxDuration: 30,
      camera: 'back',
      success(res) {
        console.log(res.tempFiles[0].tempFilePath)
        console.log(res.tempFiles[0].size)
        wx.showLoading({
          title: '上传中',
        })
        upload(res.tempFiles[0].tempFilePath).then(data => {
          console.log(data)
          that.setData({
            ['teacher.icon']: data
          })
          wx.showToast({
            title: '成功',
            icon: 'success',
            duration: 2000
          })
        }).catch(error => {
          wx.showToast({
            title: '失败',
            icon: 'error',
            duration: 2000
          })
        });
      }
    })
  },

  addContent: function (e) {
    var content = {
      type: e.currentTarget.dataset.type,
      content: ''
    }
    this.setData({
      'teacher.contents': this.data.teacher.contents.concat(content)
    });
  },

  bindTextAreaInput: function (e) {
    var index = e.currentTarget.dataset.index;
    var contents = this.data.teacher.contents;
    contents[index].content = e.detail.value;
    console.log(e.detail.value, index)
    this.setData({
      ['teacher.contents']: contents
    });
  },

  addImg: function (e) {
    var that = this
    var index = e.currentTarget.dataset.index;
    var contents = that.data.teacher.contents;
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      sourceType: ['album', 'camera'],
      maxDuration: 30,
      camera: 'back',
      success(res) {
        console.log(res.tempFiles[0].tempFilePath)
        console.log(res.tempFiles[0].size)
        wx.showLoading({
          title: '上传中',
        })
        upload(res.tempFiles[0].tempFilePath).then(data => {
          console.log(data)
          contents[index].content = data;
          that.setData({
            ['teacher.contents']: contents
          });
          wx.showToast({
            title: '成功',
            icon: 'success',
            duration: 2000
          })
        }).catch(error => {
          wx.showToast({
            title: '失败',
            icon: 'error',
            duration: 2000
          })
        });
      }
    })
  },

  submit() {
    console.log(this.data.title)
    console.log(this.data.teacher)
    wx.showLoading({
      title: '处理中',
    })
    this.setData({
      ['teacher.userId']: getApp().globalData.userInfo.userId
    })
    businessCardApi.add(this.data.teacher).then(res => {
      getApp().globalData.userInfo.avatarUrl = this.data.teacher.icon
      getApp().globalData.userInfo.userName = this.data.teacher.teacherName
      wx.showToast({
        title: '成功',
        icon: 'success',
        duration: 1500,
        success: function () {
          setTimeout(function () {
            wx.navigateBack({
              delta: 1
            });
          }, 1500);
        }
      })
    })
  },

  bindKeyInput(e) {
    this.setData({
      [`teacher.${e.currentTarget.dataset.params}`]: e.detail.value
    })
  },

  checkedChange: function (e) {
    this.setData({
      [`teacher.isOpen`]: e.detail[0]
    });
  }
})